package com.ruoyi.tb.lmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.TEquipmentLog;
import com.ruoyi.tb.lmt.domain.TbEquipmentApply;
import com.ruoyi.tb.lmt.domain.vo.TEquipmentLogVo;
import com.ruoyi.tb.lmt.domain.vo.TbEquipmentApplyVo;
import com.ruoyi.tb.lmt.mapper.TEquipmentLogMapper;
import com.ruoyi.tb.lmt.service.TEquipmentLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@RequiredArgsConstructor
@Service
public class TEquipmentLogServiceImpl implements TEquipmentLogService {
    @Autowired
    private final TEquipmentLogMapper baseMapper;

    @Override
    public TableDataInfo<TEquipmentLogVo> queryPageList(TEquipmentLog bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TEquipmentLog> lqw = buildQueryWrapper(bo);
        Page<TEquipmentLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean insertEquipmentLog(TEquipmentLog bo) {
        bo.setCreateBy(LoginHelper.getUsername());
        bo.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(bo) > 0;
    }

    @Override
    public boolean updateEquipmentLog(TEquipmentLog bo) {
        bo.setUpdateBy(LoginHelper.getUsername());
        bo.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(bo) > 0;
    }

    private LambdaQueryWrapper<TEquipmentLog> buildQueryWrapper(TEquipmentLog bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TEquipmentLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipmentId() != null, TEquipmentLog::getEquipmentId, bo.getEquipmentId());
        lqw.like(StringUtils.isNotBlank(bo.getOprtType()), TEquipmentLog::getOprtType, bo.getOprtType());
        lqw.eq(bo.getCreateTime() != null, TEquipmentLog::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TEquipmentLog::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TEquipmentLog::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TEquipmentLog::getUpdateBy, bo.getUpdateBy());
        lqw.orderByAsc(TEquipmentLog::getOprtDate);
        return lqw;
    }
}
